#ifndef __RUI_KEYRECOMMANDER_HPP__
#define __RUI_KEYRECOMMANDER_HPP__
#include "Dictionary.hpp"
#include "net/TcpConnection.hpp"
#include <iostream>
#include <queue>

using namespace std;

namespace wd{
struct MyResult{
MyResult(const string& word, int freq, int dist)
:_word(word)
,_freq(freq)
,_dist(dist){}
string _word;
int _freq;
int _dist;

};
    
struct Mycompare{
bool operator()(const MyResult& lhs, const MyResult &rhs){
    if(lhs._dist > rhs._dist){
        return true;
    }else if(lhs._dist == rhs._dist && lhs._freq < rhs._freq){
        return true;
    }else if(lhs._dist == rhs._dist && lhs._freq == rhs._freq){
        return lhs._word > rhs._word;
    }else{
        return false;
    }
}
};


class KeyRecommander{
public:
  //  KeyRecommander(string &query,const TcpConnectionPtr& conn);
      
      KeyRecommander(const string& query,  Dictionary* dictionary = Dictionary::getDictptr())
      :_queryWord(query),_dictionary(dictionary){}

      ~KeyRecommander(){}
      void doQuery(const TcpConnectionPtr&);
      
private:
      void excute(); //执行查询

#if 1
      void print(string& str){//从_resultque中打印推荐结果
          int cnt = 20;
          while(cnt-- && !_resultque.empty()){
              /* cout << _resultque.top()._word << endl; */
              str += _resultque.top()._word + ",";
              _resultque.pop();
          }
          cout<<"print() str "<<str<<endl;
      }
#endif
      void queryIndexTable();  //查询索引
      void statistic(set<int>& iset);  //计算
      int distance(const string &rhs); //计算最小距离
      void response();  //响应

      void queryforEn(const char &);
      void queryforCn(const char* p, int offset);
      int  queryforNum(const char *p);
private:
      string _queryWord;
      //TcpConnectionPtr _conn;
      std::priority_queue<MyResult, std::vector<MyResult>, Mycompare> _resultque;
      Dictionary *_dictionary;
      std::set<int> _set;
};

}

#endif
